#!/bin/bash

# 设置颜色变量 - 使用柔和的色调
RED='\033[38;2;255;85;85m'      # 柔和的红色
GREEN='\033[38;2;80;200;120m'   # 柔和的绿色
YELLOW='\033[38;2;241;196;15m'  # 柔和的黄色
BLUE='\033[38;2;30;144;255m'    # 柔和的蓝色
NC='\033[0m'                    # 重置颜色

# whiptail界面颜色设置 - 使用更现代的深蓝主题
export NEWT_COLORS='
root=,black
window=white,black
border=white,black
textbox=white,black
button=black,blue
actbutton=white,red
title=blue,black
'

# 代理列表
PROXIES=(
    "http://124.156.150.245:10086"
    "http://140.83.60.48:8081"
    "https://gh-proxy.com"
    "https://ghproxy.114514616.xyz"
    "https://mirror.ghproxy.com"
    "https://gh.api.99988866.xyz"
    "https://gh.h233.eu.org"
    "https://gh.tryxd.cn"
    ""
)

# 要过滤的目录
FILTER_DIRS=('example' 'other' 'system' 'adapter')

# 显示加载动画
show_loading() {
    local message=$1
    local frames=("◐" "◓" "◑" "◒")
    local colors=("$BLUE" "$GREEN" "$YELLOW" "$BLUE")
    local i=0
    
    (
        while true; do
            printf "\r${colors[i]}%s ${frames[i]} ${NC}" "$message"
            i=$(( (i + 1) % 4 ))
            sleep 0.12
        done
    ) &
    SPIN_PID=$!
}

# 停止加载动画
stop_loading() {
    kill $SPIN_PID 2>/dev/null
    printf "\r%*s\r" $(($(tput cols))) ""
}

# 清理代理URL - 修复版
clean_url() {
    local url=$1
    # 修复后的正则表达式
    url=$(echo "$url" | sed -E '
        s|^https?://[^/]+/https://github\.com|https://github.com|;
        s|^https?://[^/]+/github\.com|https://github.com|;
        s|^https?://gh(proxy)?[.][^/]+/|https://|;
        s|/$||
    ')
    echo "$url"
}

# 测试并获取可用代理
get_proxy() {
    local var_name="$1"
    local original_url="${!var_name}"
    local clean_url=$(clean_url "$original_url")
    local check_path="NapNeko/NapCatQQ/main/package.json"
    local speed_threshold=2
    local curl_timeout=3

    # 随机化代理列表
    local shuffled_proxies=($(printf "%s\n" "${PROXIES[@]}" | shuf))

    echo -e "${BLUE}🔍 正在检测原始URL: ${clean_url}${NC}"

    for proxy in "${shuffled_proxies[@]}"; do
        if [ -z "$proxy" ]; then
            echo -e "${RED}❌ 没有找到可用的快速代理，将使用原始URL: ${clean_url}${NC}"
            return 1
        fi

        local proxied_check_url="${proxy}/https://raw.githubusercontent.com/${check_path}"
        local result
        result=$(curl --silent --fail --max-time $curl_timeout -w "%{http_code} %{time_total}" -o /dev/null "$proxied_check_url")
        local http_code=$(echo "$result" | awk '{print $1}')
        local time_total=$(echo "$result" | awk '{print $2}')

        if [ "$http_code" = "200" ]; then
            if awk "BEGIN{exit($time_total<$speed_threshold?0:1)}"; then
                echo -e "${GREEN}✨ 已找到最佳代理: ${proxy} (响应时间: ${time_total}s)${NC}"
                echo "$proxy"
                return 0
            else
                echo -e "${YELLOW}⚠️ 代理响应较慢(${time_total}s): ${proxy}${NC}"
            fi
        else
            echo -e "${YELLOW}⚠️ 代理不可用: ${proxy}${NC}"
        fi
    done
    return 1
}

# 管理插件目录
manage_plugins() {
    local plugins_path="$yz/plugins"
    
    while true; do
        if [ ! -d "$plugins_path" ]; then
            whiptail --title "错误提示" \
                    --msgbox "❌ 插件目录不存在，请检查路径" \
                    8 40
            exit 1
        fi
        
        # 获取并过滤插件目录
        local all_dirs=($(find "$plugins_path" -mindepth 1 -maxdepth 1 -type d 2>/dev/null))
        local filtered_dirs=()
        local menu_items=()
        
        for dir in "${all_dirs[@]}"; do
            local base_name=$(basename "$dir")
            local should_filter=false
            
            for filter in "${FILTER_DIRS[@]}"; do
                if [ "$base_name" = "$filter" ]; then
                    should_filter=true
                    break
                fi
            done
            
            if [ "$should_filter" = false ]; then
                filtered_dirs+=("$dir")
                # 检查是否为git仓库
                if [ -d "$dir/.git" ]; then
                    menu_items+=("$base_name" "📦 Git仓库")
                else
                    menu_items+=("$base_name" "📁 普通目录")
                fi
            fi
        done
        
        if [ ${#filtered_dirs[@]} -eq 0 ]; then
            whiptail --title "提示信息" \
                    --msgbox "📝 未找到可管理的插件" \
                    8 40
            exit 1
        fi
        
        local selected_plugin
        selected_plugin=$(whiptail --title "插件管理" \
                                 --menu "请选择要操作的插件:" \
                                 --ok-button "确定" \
                                 --cancel-button "返回" \
                                 15 60 8 \
                                 "${menu_items[@]}" \
                                 3>&1 1>&2 2>&3)
        
        [ $? -ne 0 ] && break
        
        local selected_dir
        for dir in "${filtered_dirs[@]}"; do
            if [ "$(basename "$dir")" = "$selected_plugin" ]; then
                selected_dir="$dir"
                break
            fi
        done
        
        if [ ! -d "$selected_dir/.git" ]; then
            whiptail --title "错误提示" \
                    --msgbox "❌ ${selected_plugin} 不是Git仓库，无法管理" \
                    8 45
            continue
        fi
        
        # 获取当前远程URL
        local current_url=$(cd "$selected_dir" && git config --get remote.origin.url)
        if [ -z "$current_url" ]; then
            whiptail --title "错误提示" \
                    --msgbox "❌ 无法获取远程URL，请检查仓库配置" \
                    8 45
            continue
        fi
        current_url=$(clean_url "$current_url")
        
        show_loading "🔄 正在扫描可用代理..."
        local proxy=$(get_proxy "current_url")
        stop_loading
        
        if [ $? -eq 0 ] && [ -n "$proxy" ]; then
            if [[ "$current_url" == https://github.com/* ]]; then
                local new_url="${proxy}/${current_url}"
                if (cd "$selected_dir" && git config remote.origin.url "$new_url"); then
                    show_loading "⬇️ 正在使用代理 ${proxy} 更新插件..."
                    if (cd "$selected_dir" && git pull); then
                        stop_loading
                        whiptail --title "操作成功" \
                                --msgbox "✅ 已成功使用代理 ${proxy} 更新插件！" \
                                8 60
                    else
                        stop_loading
                        whiptail --title "错误提示" \
                                --msgbox "❌ 使用代理 ${proxy} 更新失败，请重试" \
                                8 60
                    fi
                fi
            else
                whiptail --title "提示信息" \
                        --msgbox "ℹ️ 当前URL不是GitHub链接，无法应用代理" \
                        8 45
            fi
        else
            whiptail --title "错误提示" \
                    --msgbox "❌ 未能找到可用的代理服务器" \
                    8 40
        fi
    done
}

# git命令重写函数
function git() {
    local args=("$@")
    local proxied=false
    
    for ((i=0; i<${#args[@]}; i++)); do
        if [[ "${args[i]}" == https://github.com/* || "${args[i]}" == https://raw.githubusercontent.com/* ]]; then
            args[i]=$(clean_url "${args[i]}")
            
            show_loading "🔄 正在获取最快代理..."
            local proxy=$(get_proxy "args[$i]")
            stop_loading
            
            if [ $? -eq 0 ] && [ -n "$proxy" ]; then
                args[$i]="${proxy}/${args[i]}"
                proxied=true
                echo -e "${GREEN}✅ 已启用代理: ${proxy}${NC}"
            fi
            break
        fi
    done
    
    if [ "$proxied" = true ]; then
        command git "${args[@]}"
    else
        command git "$@"
    fi
}

clear
if whiptail --title "GitHub 代理优化工具" \
            --yes-button "启动" \
            --no-button "关闭" \
            --yesno "
        🚀 GitHub 代理切换工具
            
✨ 主要功能:
 • 智能检测最快代理
 • 一键切换代理设置
 • 优化访问速度
 
🔄 开始优化之旅?" \
            15 48; then
    manage_plugins
fi
clear